package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.ws.rs.Produces;
import java.util.Map;
@RestController
@RequestMapping("/order")

public class OrderController {
    @Reference
    OrderService orderService;
    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/addOrderByTel")
    public Result addOrderByTel(@RequestBody Map map){
        map.put("orderType","手机预约");
        //验证验证码
        String telephone= (String) map.get("telephone");
        String validateCode= (String) map.get("validateCode");
        String validateCodeInRedis = jedisPool.getResource().get(telephone+RedisConstant.Validate_Code_Order);
        if (validateCodeInRedis==null||validateCode==null||!validateCodeInRedis.equals(validateCode)){
            return new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }

        try {
            return orderService.addOrder(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"网络异常，未预约成功");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map=orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
